package com.bsh.serviceImpl;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bsh.api.ApiUtils;
import com.bsh.api.params.DtoSendSms;
import com.bsh.mybatis.model.MobileVerifyCode;
import com.bsh.repository.DaoMobileVerifyCode;
import com.bsh.service.SmsService;
import com.bsh.utils.SimpleCache;
import com.bsh.utils.VerifyCodeUtil;
import com.bsh.utils.system.SmsReponseMessage;
import com.bsh.utils.system.SmsUtil;

/**
 * 短信
 * @author gsh
 *
 */
@Service
@Transactional
public class SmsServiceImpl implements SmsService {
	public static SimpleCache<Integer>cache=new SimpleCache<Integer>(600);
	private static Logger log = LoggerFactory.getLogger(SmsServiceImpl.class);
	@Autowired
	private DaoMobileVerifyCode moDao;

	/* (non-Javadoc)
	 * @see com.bsh.serviceImpl.SmsService#sendSmsVerifyCoed(java.lang.String, java.lang.String, java.lang.String)
	 */
	@Override
	public DtoSendSms sendSmsVerifyCoed(String mobile, String ip, String type,String deviceNum) {
		Integer res = -1;
		DtoSendSms result = new DtoSendSms();
		MobileVerifyCode mo = new MobileVerifyCode();
		mo.setMobile(mobile);
		mo.setIp(ip);
		mo.setCreateTime(new Date());
		mo.setType(type);
		if(ApiUtils.objIsNotEmpty(mo)){
			mo.setDeviceNum(deviceNum);
		}
		Integer code=VerifyCodeUtil.getCode6();
		Integer cacheMobileMode = null;
	    cacheMobileMode = cache.Get(mobile+type,Integer.class);

		if( cacheMobileMode == null ){
			//删除过期手机验证码 先不删除
			//moDao.deleteByMobile(mobile);
			cache.Put(mobile+type, code,1800); //时间单位 秒,1800=30分钟
			mo.setCode(Integer.toString(code));
			res = moDao.insertSelective(mo);
			result.setRes(res);
			result.setCode(code);
		}else{
			//暂时使用缓存内code ,保证如果短信延时 使用之前的验证码也可用
			result.setCode(cacheMobileMode);
			result.setRes(1);
		}
		if(result.getRes() > 0){
			SmsReponseMessage msg = SmsUtil.sendValidCode(mobile,Integer.toString(result.getCode()));
			if(msg.getCode() !=0){
				log.error("SMS_ERROR:"+msg.getCode()+"|"+msg.getMsg());
				result.setRes(-1);
				result.setMessage(msg.getMsg());
			}
		}
		
		return result;
	}
	
	/* (non-Javadoc)
	 * @see com.bsh.serviceImpl.SmsService#getCacheMobileCode(java.lang.String)
	 */
	@Override
	public Integer getCacheMobileCode(String mobile){
		Integer cacheMobileMode = null;
	    cacheMobileMode = cache.Get(mobile,Integer.class);
	    return cacheMobileMode;
	}
	
	/* (non-Javadoc)
	 * @see com.bsh.serviceImpl.SmsService#deleteCacheMobileCode(java.lang.String)
	 */
	@Override
	public void deleteCacheMobileCode(String mobile){
		cache.Put(mobile,1,0);
	}
	
	/* (non-Javadoc)
	 * @see com.bsh.serviceImpl.SmsService#getDbMobileCode(java.lang.String)
	 */
	@Override
	public Integer getDbMobileCode(String mobile){
		Integer cacheMobileMode = null;
	    cacheMobileMode = cache.Get(mobile,Integer.class);
	    return cacheMobileMode;
	}

	@Override
	public String findCodeByMobile(MobileVerifyCode record) {
		return moDao.findCodeByMobile(record);
	}
	
	/**
	 * 返回手机验证码
	 * @param mobile
	 * @param type ()
	 * @author jane
	 * @return
	 */
	@Override
	public Integer getSmsCode(String mobile,String type){
		Integer cacheMobileMode = null;
	    cacheMobileMode = cache.Get(mobile+type,Integer.class);
	    //暂时直接读cache
	    return cacheMobileMode;
	}
	

}
